package com.zscat.mallplus.qsg.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zscat.mallplus.dms.entity.DmsOrder;
import com.zscat.mallplus.dms.service.IDeviceTaskService;
import com.zscat.mallplus.enums.TaskType;
import com.zscat.mallplus.qsg.*;
import com.zscat.mallplus.qsg.mapper.QsgGatewayInfoMapper;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
@Slf4j
public class QsgGatewayInfoServiceImpl extends ServiceImpl<QsgGatewayInfoMapper, QsgGatewayInfo> implements IQsgGatewayInfoService {

    @Autowired
    private IGatewayDeviceService gatewayDeviceService;

    @Autowired
    private MqttConfigService mqttConfigService;

    @Autowired
    private IDeviceTaskService taskService;

    @Override
    public void openAllGatewayDevice(DmsOrder orderInfo) {
        QueryWrapper qw = new QueryWrapper();
        qw.eq("device_id", orderInfo.getDeviceId());
        //devices:当前房间不是喇叭的网关设备
        List<QsgGatewayDevice> devices = gatewayDeviceService.list(qw);
        if (devices.size()>0){
            for (QsgGatewayDevice currentDevice : devices){
                QsgGatewayInfo gatewayInfo = this.getById(currentDevice.getGatewayId());
                JSONObject data = new JSONObject();
                data.put("CommandId", UUID.randomUUID());
                data.put("SystemCode","csqsg");
                data.put("GatewayIeee",gatewayInfo.getSerialno());
                data.put("SensorIeee",currentDevice.getIeeeEpAddr());
                //如果是大门
                if(currentDevice.getIsDoor() == 1){
                    data.put("Command", "UnlockDoor");
                    data.put("PINCode", "1809598");
                }else if(currentDevice.getIsVoice() == 1){
                    data.put("Command","PlayVoice");
                    data.put("Index", 152 );
                }else{
                    data.put("Command","On");
                }
                try {
                    mqttConfigService.publish("/toGW/"+gatewayInfo.getMacAddr(),data.toJSONString());
                } catch (MqttException e) {
                    log.error("发送mqtt消息失败.异常:{}",e.getMessage());
                }
            }
        }
    }

    @Override
    public void generateGatewayTask(DmsOrder order,QsgDevice device) {
        QsgDeviceTask timeTask = new QsgDeviceTask();
        timeTask.setUpdateReason("网关实时订单生成");
        timeTask.setUpdateTime(new Date());
        timeTask.setMemberId(order.getMemberId().intValue());
        timeTask.setCreateDatetime(new Date());
        timeTask.setDeviceId(device.getId());
        timeTask.setDeviceCode(device.getDeviceCode());
        timeTask.setOrderSn(order.getOrderSn());
        String endTime = order.getEndTime();
        Date date = new Date(endTime);
        timeTask.setEndTime(date);
        timeTask.setTaskType(TaskType.GATEWAY_END_REPORT.getValue());
        taskService.save(timeTask);//结束任务
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MINUTE, -10);
        timeTask.setEndTime(calendar.getTime());
        timeTask.setTaskType(TaskType.GATEWAY_TEN_REPORT.getValue());
        taskService.save(timeTask);//结束前10分钟

        Calendar calfiveminute = Calendar.getInstance();
        calfiveminute.setTime(date);
        calfiveminute.add(Calendar.MINUTE, -5);
        timeTask.setEndTime(calfiveminute.getTime());
        timeTask.setTaskType(TaskType.GATEWAY_FIVE_MINUTE_REPORT.getValue());
        taskService.save(timeTask);//结束前5分钟

        Calendar calhalfhour = Calendar.getInstance();
        calhalfhour.setTime(date);
        calhalfhour.add(Calendar.MINUTE, -30);
        timeTask.setEndTime(calhalfhour.getTime());
        timeTask.setTaskType(TaskType.GATEWAY_HALF_HOUR_REPORT.getValue());
        taskService.save(timeTask);//结束前30分钟
    }
}
